package cn.exrick.xboot.modules.ReceiveSMS.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.ReceiveSMS.entity.ReceiveSMS;
import cn.exrick.xboot.modules.ReceiveSMS.service.IReceiveSMSService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author portal
 */
@Slf4j
@RestController
@Api(description = "短信接收名单管理接口")
@RequestMapping("/xboot/receiveSMS")
@Transactional
public class ReceiveSMSController {

    @Autowired
    private IReceiveSMSService iReceiveSMSService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<ReceiveSMS> get(@PathVariable String id){

        ReceiveSMS receiveSMS = iReceiveSMSService.getById(id);
        return new ResultUtil<ReceiveSMS>().setData(receiveSMS);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<ReceiveSMS>> getAll(){

        List<ReceiveSMS> list = iReceiveSMSService.list();
        return new ResultUtil<List<ReceiveSMS>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<ReceiveSMS>> getByPage(@ModelAttribute PageVo page){

        IPage<ReceiveSMS> data = iReceiveSMSService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<ReceiveSMS>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<ReceiveSMS> saveOrUpdate(@ModelAttribute ReceiveSMS receiveSMS){

        if(iReceiveSMSService.saveOrUpdate(receiveSMS)){
            return new ResultUtil<ReceiveSMS>().setData(receiveSMS);
        }
        return new ResultUtil<ReceiveSMS>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iReceiveSMSService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }


    @RequestMapping(value = "/getSMSBySys", method = RequestMethod.GET)
    @ApiOperation(value = "通过sysId获取")
    public Result<Collection<ReceiveSMS>> getSMSBySys(@ModelAttribute ReceiveSMS receiveSMS){
        Map<String,Object> map=new HashMap();
        map.put("sys_id",receiveSMS.getSysId());
        Collection<ReceiveSMS> receiveSMS1 = iReceiveSMSService.listByMap(map);
        return new ResultUtil<Collection<ReceiveSMS>>().setData(receiveSMS1);
    }
    @RequestMapping(value = "/delById", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    public Result<Object> delById(@ModelAttribute ReceiveSMS receiveSMS){

            iReceiveSMSService.removeById(receiveSMS.getId());

        return new ResultUtil<Object>().setSuccessMsg("删除数据成功");
    }


}
